package pzhu.web;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

public class PicDown {
	public void Down(String index, String name) throws ClientProtocolException, IOException {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse httpResponse = null;
		HttpGet request = new HttpGet(index);
		httpResponse = httpClient.execute(request);
		HttpEntity entity = httpResponse.getEntity();
		String html = EntityUtils.toString(entity, "gb2312");
		org.jsoup.nodes.Document document = Jsoup.parse(html);
		Elements title = document.select("div.wrap.clearfix img");
		String down = title.get(0).attr("src");
		CloseableHttpResponse httpResponse1 = httpClient.execute(new HttpGet("http://pic.netbian.com" + down));
		HttpEntity entity1 = httpResponse1.getEntity();
		java.io.InputStream inputStream = entity1.getContent();
		String path = WelcomeInterface.path.getText().replace("\\", "/");
		FileUtils.copyToFile(inputStream, new File(path + "/" + name + ".jpg"));
		WelcomeInterface.log.append("下载文件：" + name + "\n\r");
	}
}
